package com.example.administrator.news2;

import android.app.Application;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=findViewById(R.id.textView);
    }
    private Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==0){
                String text=msg.getData().getString("data");
                tv.setText(text);
            }

            return false;
        }
    });
    private void  getDataFromServer() {
        String reluts=null;
        //创建OkhttpClient
        OkHttpClient okHttpClient = new OkHttpClient();
        //构建请求信息：连接请求url 请求方法method 请求头部headers 请求体body 标签tag
        Request request = new Request.Builder().url("http://api.tianapi.com/txapi/tiangou/index?key=6341265366b1b2ba75b7a215e2fd726b").get().build();
        //同步
        try {
            Call call = okHttpClient.newCall(request);
            Response execute = call.execute();
            String success = execute.body().string();
            Log.e(TAG, "onCreate: "+success );

            reluts=fromJson(success);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Message message =new Message();
        Bundle bundle = new Bundle();
        bundle.putString("data",reluts);
        message.setData(bundle);
        message.what=0;
        handler.sendMessage(message);
//        //异步
//        okHttpClient.newCall(request).enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//
//            }
//        });
    }

    private String fromJson(String s) {
        try{
            JSONObject jsonObject = new JSONObject(s);
            JSONArray newslist= jsonObject.getJSONArray("newslist");
            s=newslist.getJSONObject(0).get("content").toString();
            Log.e(TAG, "text: "+s );
        }catch (JSONException e){
            e.printStackTrace();
        }
        return s;
    }

    public void resfult(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getDataFromServer();
            }
        }).start();
    }
    public void copy(View view) {
        String string = tv.getText().toString();
        Utils.copyToClipboard(getApplicationContext(),string);
    }
}
